package com.xliang.spider.version04.service.impl;

import com.xliang.spider.util.ConnectionUtil;
import com.xliang.spider.version04.dao.SpiderChapterDao;
import com.xliang.spider.version04.dao.SpiderLessonDao;
import com.xliang.spider.version04.pojo.ChapterEntity;
import com.xliang.spider.version04.pojo.LessonEntity;
import com.xliang.spider.version04.service.SpiderChapterService;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * ${DESCRIPTION}
 *
 * @author xiongl
 * @create 2016-06-21 15:08
 */
@Service
public class SpiderChapterServiceImpl implements SpiderChapterService
{
    private static Logger logger = Logger.getLogger(SpiderChapterServiceImpl.class);
    
    @Resource
    private SpiderLessonDao spiderScanLessonDao;
    
    @Resource
    private SpiderChapterDao spiderScanChapterDao;
    
    @Override
    public void scanChapter()
        throws Exception
    {
        LessonEntity lesson = spiderScanLessonDao.findLessonOne();
        if (lesson == null)
        {
            logger.info("所有的lesson均扫描完毕");
            return;
        }
        int page = 1;
        while (true)
        {
            int count = 0;
            logger.info("开始扫描课程：" + lesson.getLessonName() + " 的第  " + page + "  页");
            Document document = ConnectionUtil.getConnection(lesson.getLessonUrl() + "?pageNum=" + page).get();
            Elements chapters = document.getElementById("changeid").select(".cf").select("li");
            for (Element chapter : chapters)
            {
                int chapterId = Integer.parseInt(chapter.id().trim());
                String chapterName = chapter.getElementsByClass("lessonimg-box").select("img").first().attr("title").trim();
                String chapterUrl = chapter.getElementsByClass("lessonimg-box").select("a").first().attr("href").trim();
                chapterName = chapterName.replaceAll("\\\\|:|\\*|\\?|\"|<|>|\\||\u000B|/n|/r|/t", "").replaceAll("/", "&");
                ChapterEntity chapterEntity = new ChapterEntity(lesson.getLessonId(), lesson.getLessonName(), chapterId, chapterName, chapterUrl);
                try
                {
                    spiderScanChapterDao.insertChapter(chapterEntity);
                    logger.info("新增课程：" + chapterName);
                }
                catch (Exception e)
                {
                    if (e.getMessage().contains("Duplicate") && e.getMessage().contains("uniq_lesson_id_and_chapter_id_and_chapter_name"))
                    {
                        logger.info("课程：" + chapterName + " 存在，跳过");
                        count++;
                    }
                    else
                    {
                        throw new Exception(e);
                    }
                }
            }
            if (chapters.size() < 24)
            {
                break;
            }
            else if (count == 24)
            {
                break;
            }
            page++;
        }
        lesson.setStatus(1);
        spiderScanLessonDao.updateLesson(lesson);
    }
    
    public static void main(String[] args)
        throws Exception
    {
        
        Document document = ConnectionUtil.getConnection("http://www.jikexueyuan.com/course/android/?pageNum=2").get();
        Elements chapters = document.getElementById("changeid").select(".cf").select("li");
        System.out.println(chapters);
    }
}
